import datetime

from flask import Flask, request, jsonify
# 配置跨域
from flask_cors import CORS
import logging
from logging import FileHandler
# 导入蓝图包
from ceshi import tesxt
# 创建实例
from user_view import views

app = Flask(__name__)
CORS(app)
# 创造蓝图
app.register_blueprint(tesxt)
app.register_blueprint(views)

# 解决中文乱码
app.config['JSON_AS_ASCII'] = False


@app.route('/test')
def hello_world():
    str = "请求id:" + request.remote_addr + ',请求url' + request.url + "错误信息"
    app.logger.error(str)
    return 'Hello World!'


from jwttoken import Myjwt

# from tool import r
# @app.before_request
# def bdefore_request():
#     # 白名单
#     white = ['/login']
#     if request.path not in white:
#         try:
#             token = request.headers['Token']
#             print(token)
#         except:
#             return jsonify({'code': 403, 'msg': '无权访问'})
#     if Myjwt.decode_jwt(token) == False:
#         return jsonify({'code': 403, 'msg': 'token不合法'})
#     # 权限验证
#     paylad = Myjwt.decode_jwt(token)
#     uid = paylad['id']
#     key = 'serve' + str(uid)
#     list = r.find_set(key)
#     if request.path not in list:
#         return jsonify({'code': 40301, 'msg': '无权访问'})


if __name__ == '__main__':
    filename = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m')
    handler = logging.FileHandler("logs/" + filename + 'a.text')
    app.logger.addHandler(handler)
    app.run(debug=True, host='0.0.0.0', port=5000)
